package com.vca.service.service.Impl;

import com.alibaba.fastjson.JSON;
import com.vca.common.request.PaymentRequest;
import com.vca.common.response.OrderPayResultResponse;
import com.vca.common.response.PaymentPreOrderResponse;
import com.vca.common.response.UmsQueryResponse;
import com.vca.service.service.PaymentService;
import com.vca.service.service.UmsPayService;
import com.vca.service.service.VcaOrderService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description:支付API实现类
 * @author:chenbing
 * @date 2022/12/2 11:19
 */
@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private VcaOrderService vcaOrderService;

    @Autowired
    private UmsPayService umsPayService;

    /**
     * @Description:支付预下单
     * @author:chenbing
     * @date 2022/12/2 13:09
     */
    @SneakyThrows
    @Override
    public PaymentPreOrderResponse preOrder(String language,PaymentRequest request, String ip) {
        OrderPayResultResponse payment = umsPayService.payment(language,request.getOrderNo(), ip);
        PaymentPreOrderResponse paymentPreOrderResponse = new PaymentPreOrderResponse();
        paymentPreOrderResponse.setOrderNos(payment.getOrderNos());
        paymentPreOrderResponse.setPayType(payment.getPayType());
        paymentPreOrderResponse.setJsConfig(payment.getJsConfig());
        paymentPreOrderResponse.setPaymentRequire(payment.getPaymentRequire());
        return paymentPreOrderResponse;
    }

    /**
     * @Description:查询支付结果
     * @author:chenbing
     * @date 2022/12/16 17:39
     */
    @SneakyThrows
    @Override
    public UmsQueryResponse queryStatus(String language,PaymentRequest request) {
        return umsPayService.queryPayStatus(language,request.getOrderNo());
    }

    public static void main(String[] args) {
        JSON.parseObject("{\"gid\":103,\"form\":{\"fields\":[{\"name\":\"title\",\"title\":\"title\",\"value\":\"海蓝宝石\"},{\"name\":\"color\",\"title\":\"color\",\"value\":\"#59A8B8\"},{\"name\":\"description\",\"title\":\"description\",\"value\":\"代表三月,在词源学中，其命名Acquamarine源自\\\"Aqua Marina\\\"，意为\\\"海洋之水\\\"\"},{\"name\":\"link\",\"title\":\"link\",\"value\":\"www.baidu.com\"},{\"name\":\"image\",\"title\":\"image\",\"value\":\"https://vca-dev.oss-cn-hangzhou.aliyuncs.com/vcaImage/public/maintain/2022/10/26/bf831d86260d4b2990e7496181cd1ec4zlitytqy09.jpg?Expires=1686277619&OSSAccessKeyId=ULZah0Yxq41iLeaf&Signature=KXQ5vEd8ggyWsqgsxp3mM%2FxtC90%3D\"},{\"name\":\"date\",\"title\":\"date\",\"value\":[\"2023-06-01\",\"2023-06-30\"]}],\"id\":172,\"sort\":0,\"status\":true}}");
        int i=1;
    }

    @Override
    @SneakyThrows
    public Boolean close(PaymentRequest request) {
        return umsPayService.close(request.getOrderNo());
    }
}
